package edu.zju.gis.dbfg.fileserver.model;


import edu.zju.gis.dbfg.common.base.BaseEntity;
import edu.zju.gis.dbfg.fileserver.util.DateUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
@ToString
public class ConstructedDataset extends BaseEntity<String> implements Jsonable {
    private String userid;
    private String name;
    private Integer size;
    private String description;
    private String picurl;
    private String fields;
    private Date createtime;
    private Date lastmodifytime;
    private Boolean isspatialdata;
    private String datatype;
    private String storetype;
    private String departments;
    private int level;



    @Override
    public String getPk_id() {
        return this.getId();
    }

    public ConstructedDataset(List<ConstructedDataField> fields) throws  Exception{
        this.setId(UUID.randomUUID().toString());
        this.userid = "";
        this.name = "DATASET_NAME";
        this.size = -1;
        this.description = "数据描述...";
        this.picurl = "static/AddressMatch.png";
        this.fields = new JSONArray(fields).toString();
        this.createtime =DateUtil.now();
        this.lastmodifytime = this.createtime;
        this.isspatialdata = false;
        this.datatype = "Table";
        this.storetype = "ES";
        this.departments="";
        this.level=0;
    }

    public ConstructedDataset(JSONObject req) throws ParseException {
        this.userid = req.getString("userid");
        this.setId(req.getString("id"));
        this.name = req.getString("name");
        this.size = req.getInt("size");
        this.description = req.getString("description");
        this.picurl = req.getString("picurl");
        this.createtime = DateUtil.parse(req.getString("createtime"));
        this.lastmodifytime = DateUtil.parse(req.getString("lastmodifytime"));
        this.isspatialdata = req.getBoolean("isspatialdata");
        this.datatype = req.getString("datatype");
        this.storetype = req.getString("storetype");
        this.fields = req.getString("fields");
        this.departments=req.getString("departments");
        this.level=req.getInt("level");

    }

    public ConstructedDataset() throws Exception {
        this.setId(UUID.randomUUID().toString());
        this.size = -1;
        this.description = "FROM SHP...";
        this.picurl = "static/AddressMatch.png";

        this.createtime =DateUtil.now();
        this.lastmodifytime = this.createtime;
        this.isspatialdata = true;
        this.storetype = "ES";
        this.departments="";
        this.level=0;
    }
}